﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;
using THO7AlgorithmBase;

namespace THO7AlgoritmTimerApplication
{
    public partial class ExtendedForm : Form
    {
        Bitmap InputImage;
        Bitmap ResultImage;
        String LastOperationName;
        Stopwatch stopWatch;
        List<Algorithm> AlgorithmList = new List<Algorithm>();
        List<ToolStripMenuItem> ImageRelatedMenuItems = new List<ToolStripMenuItem>();
        public ExtendedForm()
        {
            InitializeComponent();
            //AlgorithmList.Add(new FlipAlgorithm("Flip"));
            //AlgorithmList.Add(new RotateAlgorithm("Rotate"));
            //AlgorithmList.Add(new InvertAlgorithm("Invert"));
            //AlgorithmList.Add(new InvertAlgorithm_4bytes("Invert (4 bytes)"));
            //AlgorithmList.Add(new InvertAlgorithm_16bytes("Invert (16 bytes)"));
            AlgorithmList.Add(new BlackOutAlgorithm("Black Out"));
            //AlgorithmList.Add(new UnsafeBlackout("Unsafe Black Out"));
            //AlgorithmList.Add(new MedianFilter("MedianFilter"));
            //AlgorithmList.Add(new SaltAndPepperNoise("Salt and Pepper Noise"));
            //AlgorithmList.Add(new WeightedColorNoise("Weighted Color Noise"));
            //Insert new Algorithms here
            /*          ^
            *          /|\
            *           |
            */
            foreach (VisionAlgorithm vA in AlgorithmList)
            {
                filterToolStripMenuItem.DropDownItems.Add(vA.Name);
            }

            ImageRelatedMenuItems.Add(closeImageToolStripMenuItem);
            ImageRelatedMenuItems.Add(saveImageAsToolStripMenuItem);
            ImageRelatedMenuItems.Add(saveImageToolStripMenuItem);
            ImageRelatedMenuItems.Add(filterToolStripMenuItem);
            ImageRelatedMenuItems.Add(imageToolStripMenuItem);
        }
        private void openImage(object sender, EventArgs e)
        {
            OpenFileDialog oFD = new OpenFileDialog();
            oFD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
            oFD.Filter = "Image Files(*.BMP;*.JPG;*.JPEG;*.PNG;)|*.BMP;*.JPG;*.JPEG;*.PNG;";
            oFD.FilterIndex = 1;
            oFD.RestoreDirectory = true;
            if (oFD.ShowDialog() == DialogResult.OK)
            {
                String CurrentFileName = Path.GetFullPath(oFD.FileName);
                InputImage = new Bitmap(CurrentFileName);
                ImageEditWindow ipf1 = new ImageEditWindow(InputImage);
                ipf1.MdiParent = this;
                ipf1.Show();
                //InputPictureBox.Image = InputImage;
                //AlgoButton.Enabled = true;
                foreach (ToolStripMenuItem tSi in ImageRelatedMenuItems)
                {
                    tSi.Enabled = true;
                }
            }
            else
            {
                //AlgoButton.Enabled = false;
            }
        }

        private void backgroundAlgorithmWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            System.ComponentModel.BackgroundWorker worker;
            worker = (System.ComponentModel.BackgroundWorker)sender;
            List<object> argList = (List<object>)e.Argument;
            VisionAlgorithm visAlg = (VisionAlgorithm)argList.ElementAt(0);
            Bitmap InputImage = (Bitmap)argList.ElementAt(1);
            ImageEditWindow iew = (ImageEditWindow)argList.ElementAt(2);
            ResultImage = visAlg.DoAlgorithm(InputImage);
            LastOperationName = visAlg.Name;
//            OutputPictureBox.Image = visAlg.DoAlgorithm(InputImage);
            stopWatch.Stop();
        }

        private void filterToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            startAlgThread(filterToolStripMenuItem.DropDownItems.IndexOf(e.ClickedItem), (ImageEditWindow)this.ActiveMdiChild);  
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void startAlgThread(int clickedItem, ImageEditWindow iew)
        {
            List<object> argList = new List<object>();
            argList.Add(AlgorithmList.ElementAt(clickedItem));
            argList.Add(iew.getCurrent());
            argList.Add(iew);
            stopWatch = new Stopwatch();
            //EnableAllInputs(false);
            stopWatch.Start();
            backgroundAlgorithmWorker.RunWorkerAsync(argList);
        }
        private void closeImageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.ActiveMdiChild.Close();
            if (this.MdiChildren.Length == 0)
            {
                foreach (ToolStripMenuItem tSi in ImageRelatedMenuItems)
                {
                    tSi.Enabled = false;
                }
            }
        }

        private void backgroundAlgorithmWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            ImageEditWindow temp = (ImageEditWindow)this.ActiveMdiChild;
            temp.addEditedPicture(ResultImage, LastOperationName);
            stopWatch.Stop();
            toolStripStatusLabel1.Text = "Last operation (" + LastOperationName + ") took " + stopWatch.ElapsedMilliseconds + "ms";
        }
    }
}
